from flask import Blueprint
from flask import Flask, redirect, request
from flask_restful import Api

from .wechat import WeChatPay, WeJsapiPay
from .wechat_login import *
from .zhipay import Pay

app = Flask(__name__)
blue = Blueprint('api',__name__)

api = Api(blue,prefix='')
api.add_resource(Pay,'/pay')
api.add_resource(WeChatPay,'/wepay')
api.add_resource(WeJsapiPay,'/weisapi')


__all__ = ['blue']

@blue.route('/wejsapi',method=['GET'])
def wejspay():
    url = 'http://o3.vc/get-weixin-code.html?appid=wxa78e72f0cb14c4cc&scope=snsapi_base&state=hello-world&redirect_uri=http%3A%2F%2Fapi.lo.o3.vc%2Fwejsapi'
    return redirect(url)



# 微信配置
weixin = WechatAPI(
    appid='wxa78e72f0cb14c4cc',
    appsecret='4322bf754ed579684e4e2cf1dd1ecf06'
)

# 登陆接口
@blue.route('/user/login')
def login():
    url = 'http://o3.vc/get-weixin-code.html?appid=wxa78e72f0cb14c4cc&scope=snsapi_userinfo&state=hello-world&redirect_uri=http%3A%2F%2Fapi.lo.o3.vc%2Fwechat%2Fauth%2Fcallback'
    # url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa78e72f0cb14c4cc&redirect_uri=http://api.lo.o3.vc/wechat/auth/callback&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect'
    return redirect(url)


# 微信的回调函数
@blue.route('/wechat/auth/callback')
def authrization():

    code = request.args.get('code')
    if not code:
        return 'code error'
    else:
        weixin.login(code)
        return redirect('http://zm.zymkshop.com/#/activity')


app.register_blueprint(blue)

if __name__ == '__main__':
    app.run()